package main

import (
	"fmt"
	"strconv"
	"sync"
)

var m = make(map[string]int)

//sync.Map
var m1 = sync.Map{}

func get(key string) int {
	return m[key]
}

func set(key string, value int) {
	m[key] = value
}

func main() {
	fmt.Println(`Go语言中内置的map不是并发安全的`)
	// wg := sync.WaitGroup{}
	// for i := 0; i < 20; i++ {
	// 	wg.Add(1)
	// 	go func(n int) {
	// 		key := strconv.Itoa(n)
	// 		set(key, n)
	// 		fmt.Printf("k=:%v,v:=%v\n", key, get(key))
	// 		wg.Done()
	// 	}(i)
	// }
	// wg.Wait()
	//sync.Map
	wg := sync.WaitGroup{}
	for i := 0; i < 20; i++ {
		wg.Add(1)
		go func(n int) {
			key := strconv.Itoa(n)
			m1.Store(key, n)
			value, _ := m1.Load(key)
			fmt.Printf("k=:%v,v:=%v\n", key, value)
			wg.Done()
		}(i)
	}
	wg.Wait()
}
